#include "PaintWidget.h"

PaintWidget::PaintWidget(QWidget *parent)
	: QWidget(parent)
{
	setMinimumSize(2000,2000);
	image = new QImage();
	selection = new QRect();
	imagePositionX = 0;
	imagePositionY = 0;
	clickTempPositionX = 0;
	clickTempPositionY = 0;
	imagePainted = false;
	moveImage = false;
	selectionColor = new QColor(0,0,255,50);
}

void PaintWidget::mousePressEvent(QMouseEvent * event)
{
	QWidget::mouseMoveEvent(event);
	button = event->button();
	selectionX = event->x();
	selectionY = event->y();
	selectionColor = new QColor(0,0,255,50);
	if(button == 2)
	{
		if(imagePainted)
		{
			if((*imageObj)->contains(selectionX - imagePositionX, selectionY - imagePositionY))
			{
				moveImage = true;
				clickTempPositionX = selectionX - imagePositionX;
				clickTempPositionY = selectionY - imagePositionY;
			}
			else moveImage = false;

		}
	}
}

void PaintWidget::mouseReleaseEvent(QMouseEvent * event)
{
	QWidget::mouseMoveEvent(event);

	//Clique gauche
	if(button == 1)
	{
		selectionWidth = event->x() - selectionX;
		selectionHeight = event->y() - selectionY;
		selection->setRect(selectionX,selectionY,selectionWidth,selectionHeight);
	}
	repaint();
}


void PaintWidget::mouseMoveEvent ( QMouseEvent * event )
{
	mousePositionX = event->x();
	mousePositionY = event->y();
	QWidget::mouseMoveEvent(event);

	//Clique gauche
	if(button == 1)
	{
		selection->setRect(selectionX,selectionY,mousePositionX - selectionX,mousePositionY - selectionY);
	}
	//Clique droit
	if(button == 2)
	{
		if(moveImage)
		{
			imagePositionX = event->x() - clickTempPositionX;
			imagePositionY = event->y() - clickTempPositionY;
			selection->setRect(0,0,0,0);
		}
	}
	repaint();
}

void PaintWidget::paintImage(ImageObj **img)
{
	imageObj = img;
	imagePainted = true;
	image = new QImage((*imageObj)->width,(*imageObj)->height,QImage::Format_RGB666);
	QRgb value;

	for(long i = (*imageObj)->height - 1; i >= 0 ; i--)
	{
		for(long j = 0; j < (*imageObj)->width; j++)
		{
			value = qRgba((*imageObj)->image[i][j].red,(*imageObj)->image[i][j].green,(*imageObj)->image[i][j].blue,(*imageObj)->image[i][j].alpha);
			//value = qRgb(255,(*imageObj)->image[i][j].green,(*imageObj)->image[i][j].blue);
			image->setPixel(j,(*imageObj)->height - 1 - i,value);
		}
	}
	repaint();
}

void PaintWidget::paintCropImage()
{
	if((*imageObj)->bitByPixel == 24) image = new QImage((*imageObj)->width,(*imageObj)->height,QImage::Format_RGB666);
	else if((*imageObj)->bitByPixel == 32) image = new QImage((*imageObj)->width,(*imageObj)->height,QImage::Format_ARGB32);
	QRgb value;

	for(long i = (*imageObj)->height - 1; i >= 0 ; i--)
	{
		for(long j = 0; j < (*imageObj)->width; j++)
		{
			value = qRgba((*imageObj)->image[i][j].red,(*imageObj)->image[i][j].green,(*imageObj)->image[i][j].blue,(*imageObj)->image[i][j].alpha);
			image->setPixel(j,(*imageObj)->height - 1 - i,value);
		}
	}
	repaint();
}


void PaintWidget::cropImage()
{
	if(!imagePainted) return;

	//Calcul des attributs du rectangle a croper
	if(selectionHeight > 0 && selectionWidth > 0)
	{
		selectionX = selectionX - imagePositionX;
		selectionY = selectionY - imagePositionY;
	}
	else if(selectionHeight < 0 && selectionWidth < 0)
	{
		selectionX = selectionX + selectionWidth - imagePositionX;
		selectionY = selectionY + selectionHeight - imagePositionY;
	}
	else if(selectionHeight < 0 && selectionWidth > 0)
	{
		selectionX = selectionX - imagePositionX;
		selectionY = selectionY + selectionHeight - imagePositionY;
	}
	else if(selectionHeight > 0 && selectionWidth < 0)
	{
		selectionX = selectionX + selectionWidth - imagePositionX;
		selectionY = selectionY - imagePositionY;
	}

	//Si la totalit? de la selection est en dehors de l'image
	if(selectionX + abs(selectionWidth) <= 0 || selectionY + abs(selectionHeight) <= 0)
	{
		selectionX = 0;
		selectionY = 0;
		selectionWidth = 0;
		selectionHeight = 0;
		return;
	}
	if(selectionX >= (long)((*imageObj)->width) || selectionY >= (long)((*imageObj)->height))
	{
		selectionX = 0;
		selectionY = 0;
		selectionWidth = 0;
		selectionHeight = 0;
		return;
	}

	//Calcul de l'image a croper
	(*imageObj)->cropRectangle(selectionX,abs(selectionWidth),selectionY,abs(selectionHeight));
	selectionX = (*imageObj)->getCropX();
	selectionY = (*imageObj)->getCropY();
	selectionWidth = (*imageObj)->getCropWidth();
	selectionHeight = (*imageObj)->getCropHeight();

	if(selectionX >= 0 && selectionY >= 0 && selectionWidth > 0 && selectionHeight > 0)
	{

		selection->setRect(selectionX + imagePositionX,selectionY + imagePositionY,selectionWidth,selectionHeight);
		selectionColor = new QColor(255,0,0,50);
		repaint();
	
		long answer = QMessageBox::question(this, tr("Resize"), tr("Etes-vous s?re de vouloir redimmensionner l'image aux dimensions du rectangle rouge?"), QMessageBox::Yes | QMessageBox::No);
		if (answer == QMessageBox::Yes)
		{
			RGBColor** imageCrop = new RGBColor*[selectionHeight];
			for(long i = 0; i < selectionHeight; i++)
			{
				imageCrop[i] = new RGBColor[selectionWidth];
				for(long j = 0; j < selectionWidth; j++)
				{
					if(!((*imageObj)->contains(selectionX + j, selectionY + i))) continue;

					byte red = (*imageObj)->image[(*imageObj)->height - selectionY - selectionHeight + i][selectionX + j].red;
					byte green = (*imageObj)->image[(*imageObj)->height - selectionY - selectionHeight + i][selectionX + j].green;
					byte blue = (*imageObj)->image[(*imageObj)->height - selectionY - selectionHeight + i][selectionX + j].blue;
					byte alpha = (*imageObj)->image[(*imageObj)->height - selectionY - selectionHeight + i][selectionX + j].alpha;

					RGBColor* couleur = new RGBColor(red,green,blue,alpha);
					imageCrop[i][j] = *couleur;
				}
			}

			(*imageObj)->cropImage(selectionWidth, selectionHeight, imageCrop);
			this->paintCropImage();
			imagePositionX = selectionX + imagePositionX;
			imagePositionY = selectionY + imagePositionY;
			repaint();
		}
		if (answer == QMessageBox::No)
		{
			selectionColor = new QColor(0,0,255,50);
		}
	}

}


void PaintWidget::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	QWidget::paintEvent(event);
	painter.setRenderHint(QPainter::Antialiasing, true);

	if(image != NULL)
	{
		painter.drawImage(QRect(imagePositionX,imagePositionY,image->width(),image->height()),*image);
	}
	if(selection != NULL)
	{
		QPen pen(*selectionColor);
		//pen.setStyle(Qt::DotLine);
		pen.setWidth(0.8);
		pen.setJoinStyle(Qt::MiterJoin);
		//pen.setCapStyle(Qt::SquareCap);
		painter.setPen(pen);
		painter.setBrush(*selectionColor);
		painter.drawRect(*selection);
		
	}
}
